<template>
  <a-modal
    title="新增POI数据"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <div class="modal-content">
        <a-form :form="form">
          <a-form-item
            label="POI数据时间"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback
          >
            <a-date-picker style="width: 100%" placeholder="请选择时间" v-decorator="['uploadDate',{rules: [{ required: true, message: '请选择时间！' }]}]"/>
          </a-form-item>
          <div class="modal-upload" >
            <upload ref="upload" @success="handleUploadSuccess" :accept="accept" remark="请选择csv文件"/>
          </div>
        </a-form>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { poiDataImport } from '@/api/modular/main/poidata/poiDataManage'
  import upload from '@/views/main/tsdatarecord/upload'
  export default {
    components: {
      upload
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        accept: '.csv',
        createDateDateString: '',
        modifyDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
      },
      handleUploadSuccess(result) {
        this.fileInfo = result
        this.handleSubmit()
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        if (!this.fileInfo) {
          this.$message.error('请选择需要导入的文件')
          return
        }
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            this.fileInfo.year = values.uploadDate.year()
            this.fileInfo.month = values.uploadDate.month() + 1
            this.fileInfo.date = values.uploadDate.date()

            poiDataImport(this.fileInfo).then((res) => {
              if (res.success) {
                this.$message.success('文件导入成功')
                this.confirmLoading = false
                this.$emit('ok')
                this.handleCancel()
              } else {
                this.$message.error('文件导入失败' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangecreateDate(date, dateString) {
        this.createDateDateString = dateString
      },
      onChangemodifyDate(date, dateString) {
        this.modifyDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.$refs.upload.reset()
        this.visible = false
      }
    }
  }
</script>
<style scoped lang="less">
.ant-upload-drag-icon {
  margin: 29px 0 20px 0;
}
.modal-content {

}
.modal-upload {
  padding: 10px 0;
}
.modal-table {
  padding: 10px 0;
}
.modal-save {
  padding: 10px 0;
}

p.download-link {
  margin-bottom: 30px;
}

.percent-wrapper {
  background: #fafafa;
  border: 1px dashed #d9d9d9;
  border-radius: 2px;
  padding: 93px 50px 94px;
  text-align: center;
}

.file-name {
  color: rgba(0, 0, 0, 0.4);
}

.temp-exception {
  color: red;
  font-size: 12px;
}

span.temp-reupload {
  margin-left: 10px;
  font-size: 12px;
}
</style>
